﻿// ActionScript file
/**
 * @usage 滚动条初始化类
 * 
 * @experience
 * 当scrollContentHeight不是绝对赋值时，即通过[content].height-[mask].height动态取得时，需注意以下几点：
 * 1、包含静态文本的MovieClip的实际宽高与显示宽高会发生偏差，
 * 	如实际高度为499，[mc].height则为561.35,此会造成滚动距离偏差.
 * 2、遮罩被缩放后(scaleX/scaleY)发生改变时，会取不到显示宽高.
 * 
 * <code>
 * UI视图中scrollbar名字必须为sb,滚动按钮name为thumb,轨迹name为track;
 * thumb&track.y/x必须为0;content.y/x必须为0;
 * 
 * [ScrollBox].stageInstance = [ScrollBox].stage;
 * [ScrollBox].scrollContent = [ScrollBox].imgMc;
 * [ScrollBox].scrollContentHeight = [ScrollBox].content_mc.height - [ScrollBox].[mask_mc].height;
 * </code>
 */
package org.nickro.project.objectInit{
	import flash.display.MovieClip;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	
	import org.nickro.project.events.ScrollEvent;
	import org.nickro.externallibrary.tween.TweenLite;
	import org.nickro.externallibrary.tween.easing.*;
	public class ScrollBox extends EventDispatcher
	{
		public static const MOVE:String = "move";
		
		private static var yMax:Number;
		private static var yMin:Number;
		private static var yOffset:Number;
		
		private static var sb:MovieClip;
		//滚动bar的高度
		private static var _scrollBoxHeight:uint = 0;
		/**
		 * @param scrollContent	滚动目标
		 */
		private static var _scrollContent:*;
		/**
		 * @param	scrollContentHeight	目标滚动的高度
		 */
		private static var _scrollContentHeight:Number;
		
		public function ScrollBox()
		{
			
		}
		
		public static function initScroll(content:MovieClip,height:Number,scrollBar:MovieClip):void
		{
			_scrollContent = content;
			_scrollContentHeight = height;
			sb = scrollBar;
			
			yMax = (sb.track.height - sb.thumb.height);
			yMin = 0;
			sb.thumb.addEventListener(MouseEvent.MOUSE_DOWN,onThumbDown);
			sb.thumb.buttonMode = true;
			if(_scrollContent.stage == null)
			{
				throw new Error("Stage isn't unExisted");
			}else
			{
				_scrollContent.stage.addEventListener(MouseEvent.MOUSE_UP,onThumbUp);
			}
		}
		
		private static function onThumbDown(e:MouseEvent):void
		{
			_scrollContent.stage.addEventListener(MouseEvent.MOUSE_MOVE, onThumbMove);
			trace("ok");
			yOffset = (e.target.mouseY - sb.thumb.y);
		}
		
		private static function onThumbMove(e:MouseEvent):void
		{
			var percent:Number;
			sb.thumb.y = e.target.mouseY - yOffset;
			if(sb.thumb.y <= yMin)
			{
				sb.thumb.y = yMin;
			}
			if(sb.thumb.y >= yMax)
			{
				sb.thumb.y = yMax;
			}
			percent = (sb.thumb.y / yMax);
			TweenLite.to(_scrollContent,1,{y:-((percent * (_scrollContentHeight - _scrollBoxHeight))),onUpdate:sendRenderEvent});
			e.updateAfterEvent();
		}
		
		private static function sendRenderEvent():void
		{
			//dispatchEvent(new ScrollEvent(ScrollEvent.MOVE,_scrollContent.y));
		}
		
		private static function onThumbUp(e:MouseEvent):void
		{
			_scrollContent.stage.removeEventListener(MouseEvent.MOUSE_MOVE,onThumbMove);
		}
		
		public static function destroyScroll():void
		{
			trace("ScrollBox: destroyScroll()");
            sb.thumb.removeEventListener(MouseEvent.MOUSE_DOWN, onThumbDown);
            _scrollContent.stage.removeEventListener(MouseEvent.MOUSE_UP, onThumbUp);
            _scrollContent.stage.removeEventListener(MouseEvent.MOUSE_MOVE, onThumbMove);
		}
	}
}